var paperId = "";
var arrayObj = new Array();
var arrayTitleNo = new Array();
var singleChoose;
var multichoose;
var judgementChoose;
var arrStr = new Array();
var titleType = "";
var flag = "radio";
var arrAnswer=new Array();
var strType="";
$(function() {
	var thisURL = document.URL;
	var getval = thisURL.split('?')[1];
	var paperId = getval.split("=")[1];
	$.ajax({
		url : "../PaperController/findByType.action",
		dataType : 'json',
		async : false,
		data : {
			"paperId" : paperId
		},
		
		success : function(data) {
			titleType = data[0];
			
		},
	});
	
	$.ajax({
		url : "../PaperController/findByCondition.action",
		dataType : 'json',
		async : false,
		data : {
			"paperId" : paperId
		},
		success : function(data) {
			$.each(data, function(i, n) {
				$("#paperName").val(n.paperName);
				$("#number").val(n.number + 0 + n.multipltChoose + 0+ n.judgementChoose);
				$("#time").val(n.time);
				$("#time1").val(n.time);
				singleChoose = n.number / titleType.length;
				multichoose = n.multipltChoose /titleType.length;
				judgementChoose = n.judgementChoose / titleType.length;
				
			});
		},
	});

	
	$.ajax({
				url : "../PaperTitleController/findByCondition.action",
				dataType : 'json',
				async : false,
				data : {
					"paperId" : paperId
				},
				success : function(data) {
					if (singleChoose > 0) {
						var str1 = "<h4> &nbsp; &nbsp; 单选题（只有一个正确选项）</h4>"
					} else {
						var str1 = "";
					}
					if (multichoose > 0) {
						var str2 = "<h4> &nbsp; &nbsp; 多选题（可以有多个正确选项）</h4>"
					} else {
						var str2 = "";
					}
					if (judgementChoose > 0) {
						var str3 = "<h4>&nbsp; &nbsp;判断题部分</h4>"
					} else {
						var str3 = "";
					}
					var str = "";
					var str = '';
					var paperNum = data.length;
					var i = 0;
					for (i = 0; i < paperNum; i++) {
						if (typeof (titleType[i/(paperNum/titleType.length)]) == "undefined") {
							str = "";						
							if (i % (paperNum/titleType.length) == singleChoose) {
								str += str2;
								flag = "checkbox";
							}
							if (i % (paperNum/titleType.length) == singleChoose + 0
									+ multichoose) {
								str += str3;
								flag = "radio";
							}							
						} else {
							str = '<div id="'+i /(paperNum/titleType.length)+'"><h2>&nbsp; &nbsp;' + titleType[i /(paperNum/titleType.length)]
									+ '</h2></div>';
							
							strType = '<a href="#'+i /(paperNum/titleType.length)+'">&nbsp; &nbsp;' + titleType[i /(paperNum/titleType.length)]
							+ '</a>';
							$("#titleType").append(strType);
							$("#titleType1").append(strType);
							
							if (i % (paperNum/titleType.length)== 0) {
								str += str1;
								flag = "radio";
							}
						}
						str += '<ul style="background:rgb(199,237,204);width:96%">'
								+ '<div style="height：30px;font-size:18px;">'
								+ '<input type="hidden" id="paperNum" value="'
								+ paperNum
								+ '">'
								+ '<input type="hidden" id="titleId" name="titleId" value="'
								+ paperId
								+ '">'
								+ '<span>'
								+ data[i].extend2
								+ '、</span>'
								+ '<strong>'
								+ data[i].titleName
								+ '</strong>'
								+ '</div>'
								+ "<li>"
								+ '<div style="font-size:16px;margin-top:15px;" id="'
								+ data[i].extend2
								+ '_div1'
								+ '">'
								+ '<input type="'
								+ flag
								+ '" id="'
								+ data[i].extend2
								+ '_1'
								+ '" name="'
								+ data[i].extend2
								+ '" style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;" value="A">'
								+ '<label for='
								+ data[i].extend2
								+ '_1'
								+ '>'
								+ "A："
								+ data[i].answera
								+ '</label>'
								+ '</div>'
								+ '</li>'
								+ "<li>"
								+ '<div  style="font-size:16px;margin-top:5px;id="'
								+ data[i].extend2
								+ '_div2'
								+ '">'
								+ '<input type="'
								+ flag
								+ '" id="'
								+ data[i].extend2
								+ '_2'
								+ '" name="'
								+ data[i].extend2
								+ '"  style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;" value="B">'
								+ '<label for='
								+ data[i].extend2
								+ '_2'
								+ '>'
								+ "B："
								+ data[i].answerb
								+ '</label>'
								+ '</div>'
								+ '</li>'
							   var string="<li>"
								+ '<div  style="font-size:16px;margin-top:5px;id="'
								+ data[i].extend2
								+ '_div3'
								+ '">'
								+ '<input type="'
								+ flag
								+ '" id="'
								+ data[i].extend2
								+ '_3'
								+ '" name="'
								+ data[i].extend2
								+ '"  style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;" value="C">'
								+ '<label style="font-size:16px" for='
								+ data[i].extend2
								+ '_3'
								+ '>'
								+ "C："
								+ data[i].answerc
								+ '</label>'
								+ '</div>'
								+ '</li>'
								+ "<li>"
								+ '<div  style="font-size:16px;margin-top:5px;id="'
								+ data[i].extend2
								+ '_div4'
								+ '">'
								+ '<input type="'
								+ flag
								+ '" id="'
								+ data[i].extend2
								+ '_4'
								+ '" name="'
								+ data[i].extend2
								+ '"  style="vertical-align:middle; margin-top:-2px; margin-bottom:1px;" value="D">'
								+ '<label  for='
								+ data[i].extend2
								+ '_4'
								+ '>'
								+ "D："
								+ data[i].answerd
								+ '</label>'
								+ '</div>' + '</li>' ;
								var string1= '</br>' + '</ul>';
								if (typeof (titleType[i/(paperNum/titleType.length)]) == "undefined") {
									var a=i % (paperNum/titleType.length) -(singleChoose+0+multichoose);
									if (a>=0&&a<judgementChoose) {
										string="";
									}
								} 
						$('#divPaper01').append(str+string+string1);
						var name = data[i].extend2;
						$('#titleNo').val(name);
						arrayObj[i] = name;
						arrayTitleNo.push(data[i].titleId);
					}
				}
			});
	
	var maxtime = $("#time").val() * 60;
	$("#beginTest").click(function() {
		$("#beginTest").css("display", "none");
		$("#userTitleForm").css("display", "block");
		$("#nav").css("display","block");
		$("#test1").css("display","none");
		$("#header").css("display","none");
		timer = setInterval(function CountDown() {
			// 一个小时，按秒计算，自己调整!
			if (maxtime >= 0) {
				minutes = Math.floor(maxtime / 60);
				seconds = Math.floor(maxtime % 60);
				msg = "距离结束还有" + minutes + "分" + seconds + "秒";
				document.getElementById("timer").innerHTML = msg;
				document.getElementById("timer1").innerHTML = msg;
				if (maxtime == 5 * 60)
					alert('注意，还有5分钟!');
				--maxtime;
			} else {
				clearInterval(timer);
				alert("时间到，结束!");
				$("#userTitleForm").submit();
			}
		}, 1000);

	});

	$("#submitUserPaper").click(
			function() {
               var str="";
				var array = new Array();
				for (var i = 0; i < arrayObj.length; i++) {
					if (typeof (titleType[i / titleType.length]) == "undefined"&& i % titleType.length == singleChoose) {		                       																			
						str="";
						$('input:checkbox[name="' + arrayObj[i] + '"]:checked').each(function() {
						   str+=$(this).val()+" ";
					        });
					} 
					else {
						str = $('input:radio[name="' + arrayObj[i]+ '"]:checked').val();	
					}
					array.push(str); 
				}
				
				$("#userAnswer").val(array);
				$("#titleList").val(arrayTitleNo);
				alert("您已成功交卷，稍后可查看您的测评报告");
				$("#userTitleForm").submit();
			});

});
